package com.roocon.thread.ExceptionHandler;

import java.util.concurrent.*;

public class FutureTaskDemo {
  public static void main(String[] args) {

    //        1.one thread
    //thread();
    pool();
  }

  private static void pool() {
    //1. 创建线程池
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    //2.创建Callable，有返回值的，你也可以创建一个线程实现Callable接口。
    //  如果你不需要返回值，这里也可以创建一个Thread即可，在第3步时submit这个thread。
    Callable<Integer> callable = new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return 1 / 0;
      }
    };
    //3.提交待执行的线程
    Future<Integer> future = executorService.submit(callable);
    try {
      Integer result = future.get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      //4.处理捕获的线程异常
      e.printStackTrace();
    }
  }

  private static void thread() {
    //1.创建FeatureTask
    FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        return 1 / 0;
      }
    });
    //2.创建Thread
    Thread thread = new Thread(futureTask);
    //3.启动线程
    thread.start();
    try {
      Integer result = futureTask.get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      //4.处理捕获的线程异常
      e.printStackTrace();
    }
  }
}
